/**
 * 初始化数据源定义详情对话框
 */
var SdcTreatTimeQuantum = {

    validateFields: {}
};

/**
 * 初始化
 */
SdcTreatTimeQuantum.init = function () {
    var params = {};
    params.id = $("#id").val();
    var ajax = new $ax(Sys.backendPath + "/sdc/sdctreatplan/showTimeQuantum", function (data) {
        $("#dataId").combotree("setValue",data.data);
        $("#dicId").combotree("setValue",data.dic);
        $("#getTiming").combobox("select", data.getTiming);
        $("#timeUnit").val(data.timeUnit);
        $("#startTime").val(data.pushStart);
        $("#endTime").val(data.pushEnd);
    },function (data){});
    ajax.setData(params);
    ajax.start();
};

/**
 * 验证数据是否为空
 */
SdcTreatTimeQuantum.validate = function () {
    $('#SdcTreatTimeQuantumForm').data("bootstrapValidator").resetForm();
    $('#SdcTreatTimeQuantumForm').bootstrapValidator('validate');
    return $("#SdcTreatTimeQuantumForm").data('bootstrapValidator').isValid();
}

/**
 * 提交新增
 */
SdcTreatTimeQuantum.addSubmit = function () {
    if (!this.validate()) {
        return;
    }
    // var errinfo = "";
    // var dataId = $("#dataId").combotree('getValue');
    // var dicId =  $("#dicId").combotree('getValue');
    // var getTiming = Common_GetValue('getTiming');
    // var timeUnit = Common_GetValue('timeUnit');
    // if (!dataId) {
    //     errinfo = errinfo + "数据源为空!<br>";
    // }
    // if (!dicId) {
    //     errinfo = errinfo + "数据字典为空!<br>";
    // }
    // if (!getTiming) {
    //     errinfo = errinfo + "采集时机为空!<br>";
    // }
    // if (!timeUnit) {
    //     errinfo = errinfo + "计算单位为空!<br>";
    // }
    // if (errinfo) {
    //     $.messager.alert("错误提示", errinfo, 'info');
    //     return;
    // }
    var queryData = {};
    var ajax = new $ax(Sys.backendPath + "/sdc/sdctreatplan/addTimeQuantum", function (data) {
        //SdsDataConfig.initSourceForm();
        Sys.success("保存成功!");
        window.parent.SdcTreatPlan.table.refresh();
        SdcTreatPlanInfoDlg.close();
    }, function (data) {
        Sys.error("保存成功!" + data.responseJSON.message + "!");
    });
    queryData['id'] = $("#id").val();
    queryData['dataId'] = $("#dataId").combotree('getValue');
    queryData['dicId'] = $("#dicId").combotree('getValue');
    queryData['getTiming'] = Common_GetValue('getTiming')
    queryData['timeUnit'] = $("#timeUnit").combotree('getValue');
    queryData['pushStart'] = $("#startTime").val();
    queryData['pushEnd'] = $("#endTime").val();
    ajax.setData(queryData);
    ajax.start();
}

/**
 * 提交修改
 */
SdcTreatTimeQuantum.editSubmit = function () {


}

function Common_GetValue() {
    var itmValue = '';

    if (typeof(arguments[0]) !== 'string') return '';
    if (arguments[0] == '') return '';

    var $this = $('#'+ arguments[0]);
    if ($this.length < 1) return '';

    var className = $this.attr("class").split(' ')[0];

    if (className == 'textbox') {  //文本框
        itmValue = $this.val();
    }else if (className == 'hisui-numberbox') {  //数字
        itmValue = $this.val();
    }else if (className == 'hisui-datebox') {  //日期
        itmValue = $this.datebox('getValue');
    }else if (className == 'hisui-timespinner') {  //时间框
        itmValue = $this.timespinner('getValue');
    }else if (className == 'hisui-combobox') {  //下拉框（多选下拉框没有封装）
        itmValue = $this.combobox('getValue');
    }else if (className == 'hisui-switchbox') {  //开关
        itmValue = $this.switchbox('getValue');
    }else if (className == 'hisui-checkbox') {  // 单个复选框
        itmValue = $this.checkbox('getValue');
    }else if (className == 'hisui-radio') {  //单个单选框
        itmValue = $this.radio('getValue');
    }else if (className == 'hisui-searchbox') {  //查询框框
        itmValue = $this.searchbox('getValue');
    }

    return itmValue;
}

$(function () {
    $HUI.combobox('#calUnit',
        {
            data:[
                {'calUnit':'1','Desc':"年"},
                {'calUnit':'2','Desc':"月"},
                {'calUnit':'3','Desc':"日"},
                {'calUnit':'4','Desc':"时"},
                {'calUnit':'5','Desc':"分"},
                {'calUnit':'6','Desc':"秒"},
            ],
            valueField:'calUnit',
            textField:'Desc',
        })

    $HUI.combotree('#dataId', {
        url: '/sds/sdsstanddatasource/getDataSourceTree'
    });

    $HUI.combotree('#dicId', {
        url: '/sds/sdsstanddic/getDicTree/' + "0"
    });

    Sys.initValidator("SdcTreatTimeQuantumForm", SdcTreatTimeQuantum.validateFields);

    SdcTreatTimeQuantum.init();
});
